package me.baicai.modules.belldandy.gcb.rest;

import me.baicai.aop.log.Log;
import me.baicai.modules.belldandy.gcb.domain.UserProject;
import me.baicai.modules.belldandy.gcb.service.UserProjectService;
import me.baicai.modules.belldandy.gcb.service.dto.UserProjectQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author 超级大白菜
* @date 2020-04-01
*/
@Api(tags = "UserProject管理")
@RestController
@RequestMapping("/api/userProject")
public class UserProjectController {

    private final UserProjectService userProjectService;

    public UserProjectController(UserProjectService userProjectService) {
        this.userProjectService = userProjectService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('userProject:list')")
    public void download(HttpServletResponse response, UserProjectQueryCriteria criteria) throws IOException {
        userProjectService.download(userProjectService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询UserProject")
    @ApiOperation("查询UserProject")
    @PreAuthorize("@el.check('userProject:list')")
    public ResponseEntity getUserProjects(UserProjectQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(userProjectService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增UserProject")
    @ApiOperation("新增UserProject")
    @PreAuthorize("@el.check('userProject:add')")
    public ResponseEntity create(@Validated @RequestBody UserProject resources){
        return new ResponseEntity<>(userProjectService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改UserProject")
    @ApiOperation("修改UserProject")
    @PreAuthorize("@el.check('userProject:edit')")
    public ResponseEntity update(@Validated @RequestBody UserProject resources){
        userProjectService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping(value = "/{pid}")
    @Log("删除UserProject")
    @ApiOperation("删除UserProject")
    @PreAuthorize("@el.check('userProject:del')")
    public ResponseEntity delete(@PathVariable Integer pid){
        userProjectService.delete(pid);
        return new ResponseEntity(HttpStatus.OK);
    }
}
